import pandas as pd

# Function to calculate Cronbach's Alpha
def cronbach_alpha(data):
    items_variance = data.var(axis=0, ddof=1)  # Variance of each item
    total_variance = data.sum(axis=1).var(ddof=1)  # Variance of the total score
    return (data.shape[1] / (data.shape[1] - 1)) * (1 - items_variance.sum() / total_variance)

# Load the cleaned and preprocessed survey data
file_path = r"D:\RENJIN RAJU\MASTERS\SEMESTER 3\THESIS\Research\Analysis\Survey data cleaned.csv"  # Update this with your file path
survey_data = pd.read_csv(file_path, encoding='ISO-8859-1')  # Adjust encoding if necessary

# Define columns related to technology adoption
tech_columns = ['adoption_Artificial_Intelligence_AI', 'adoption_Internet_of_Things_IoT',
                 'adoption_Blockchain', 'adoption_Cloud_Computing', 'adoption_Robotic_and_Automation',
                 'adoption_Big_Data_Analytics', 'adoption_Simulation', 'adoption_Other'
]

# Define columns related to barriers to adoption
barriers_columns = ['barriers_High_Implementation_Costs', 'barriers_Lack_of_Skilled_Workforce',
                    'barriers_Integration_Issues', 'barriers_Resistance_to_Change'
]

# Define columns related to supply chain resilience
resilience_columns = [
    'Disruption_Natural_Disasters', 'Disruption_Supplier_Failures',
    'Disruption_Cybersecurity_Threats', 'Disruption_Global_Crises',
    'Disruption_Transportation_Delays',
    'response_Increased_Inventory_Buffers', 'response_Diversified_Suppliers',
    'response_Implemented_New_Technologies', 'response_Other'
]

# Define columns related to collaboration and decision-making
collaboration_columns = [
    'visibility_and_control_Rank_1',
    'visibility_and_control_Rank_2',
    'visibility_and_control_Rank_3',
    'visibility_and_control_Rank_4',
    'visibility_and_control_Rank_5',
    'visibility_and_control_Rank_6'
]

# Extract the relevant data for each construct
tech_data = survey_data[tech_columns].apply(lambda x: x.notna().astype(int))  # Convert to binary (1 if selected, 0 if not)
barriers_data = survey_data[barriers_columns].apply(lambda x: x.notna().astype(int))  # Convert to binary (1 if selected, 0 if not)
resilience_data = survey_data[resilience_columns].apply(lambda x: x.notna().astype(int))  # Convert to binary (1 if selected, 0 if not)
collaboration_data = survey_data[collaboration_columns].apply(lambda x: x.notna().astype(int))  # Convert to binary (1 if selected, 0 if not)

# Calculate Cronbach's Alpha for each construct
tech_alpha = cronbach_alpha(tech_data)
barriers_alpha = cronbach_alpha(barriers_data)
resilience_alpha = cronbach_alpha(resilience_data)  # This might return NaN if no variation
collaboration_alpha = cronbach_alpha(collaboration_data)

# Output the results
print(f"Cronbach's Alpha for Technology Adoption: {tech_alpha}")
print(f"Cronbach's Alpha for Barriers to Adoption: {barriers_alpha}")
print(f"Cronbach's Alpha for Supply Chain Resilience: {resilience_alpha}")
print(f"Cronbach's Alpha for Collaboration and Decision-Making: {collaboration_alpha}")
